/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.provider;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.xbdun.dao.bmp.entity.XbdunSysOrgDo;

/**
 * 动态XbdunSysOrgDo的SQL语句提供者
 * 企业信息
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
public class SysOrgSqlProvider {

	//日志
	Log log = LogFactory.getLog(SysOrgSqlProvider.class);
	
	/**
	 * 动态sql更新对象
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateDynXbdunSysOrgSql(Map<String,Object> map){
		
		StringBuilder sbSql = new StringBuilder();
		Map<String,Object> params = (Map<String, Object>) map.get("param");
		String _where = constWhere(map);
		boolean bl = false;
		
		Assert.isTrue(StringUtils.isNotEmpty(_where),"动态更新操作，条件Where不存在");
		
		//移除Where条件
		sbSql.append("UPDATE xbdun_sys_org SET ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = params.entrySet().iterator();
			while(iparam.hasNext()){
				if(bl)sbSql.append(",");
				
				Entry<String, Object> enty = iparam.next();
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
			//设置Where条件
			if(!StringUtils.isEmpty(_where)){
				sbSql.append(" WHERE "+_where);
			}
		}else{
			//参数为空则，清空所有的SQL
			sbSql.setLength(0);
		}
		String sqlContent = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return sqlContent;
	}

	/**
	 * 判断是否存在,不为null则是存在
	 * @param map
	 */
	public String existSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		@SuppressWarnings("unchecked")
		Map<String,Object> mparam = (Map<String,Object>)params.get("param");
		boolean bl = false;
		sbSql.append("SELECT count(1) FROM xbdun_sys_org WHERE ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = mparam.entrySet().iterator();
			while(iparam.hasNext()){
				
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
		}
		
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
	/**
	* @Title: existSysOrgSql
	* @Description: 判断企业是否存在
	* @param @param xbdunSysOrgDo
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	public String existSysOrgSql(XbdunSysOrgDo xbdunSysOrgDo){
		boolean isAdd = false;
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("SELECT COUNT(*) FROM xbdun_sys_org WHERE");
		if(StringUtils.isNotBlank(xbdunSysOrgDo.getOrgEmail())){
			sbSql.append("	orgEmail = #{orgEmail}");
			isAdd = true;
		}
		
		if(StringUtils.isNotBlank(xbdunSysOrgDo.getOrgName())){
			if( isAdd ) sbSql.append(" OR ");
			sbSql.append(" orgName = #{orgName}");
			isAdd = true;
		}
		
		if(StringUtils.isNotBlank(xbdunSysOrgDo.getRecordNo())){
			if( isAdd ) sbSql.append(" OR ");
			sbSql.append(" recordNo = #{recordNo}");
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	/**
	 * 查询分页
	 * @param params
	 * @return
	 */
	public String queryPagerSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//排序规则
		String orderBy = (String) params.get("_orderby");
		//当前页
		int pageIndex = (Integer) params.get("pageIndex");
		//显示条数
		int pageSize = (Integer) params.get("pageSize");

		sbSql.append("SELECT * ");
		sbSql.append(" FROM xbdun_sys_org WHERE 1=1 ");
		constSysOrgWhere(sbSql,params);
		//查看排序
		if(!StringUtils.isEmpty(orderBy)){
			sbSql.append(" ORDER BY "+orderBy);
		}
		
		//分页
		if(pageSize>0){
			sbSql.append(" LIMIT "+pageIndex+","+pageSize);
		}
		
		String contentSql = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	/**
	 * 查询分页查询总条数
	 * @param params
	 * @return
	 */
	public String queryPagerCountSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		
		sbSql.append("SELECT COUNT(*) ");
		sbSql.append(" FROM xbdun_sys_org WHERE 1=1 ");
		constSysOrgWhere(sbSql,params);
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	// 生成分页查询条件
	@SuppressWarnings("unchecked")
	private void constSysOrgWhere(StringBuilder sbSql,Map<String, Object> params) {
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		// 审核状态
		if(null != _where.get("auditingStatus")){
			//审核状态为0,默认为不等于审核通过的
			sbSql.append(" AND auditingStatus = #{_where.auditingStatus}");
			
			String serchColumn = "createTime";
			//申请状态
			if (((Integer)_where.get("auditingStatus")).intValue() != 1) {
				serchColumn = "auditingTime";
			}
			if(null != _where.get("startDate")){
				sbSql.append(" AND "+serchColumn+" >= #{_where.startDate}");
			}
			if(null != _where.get("endDate")){
				sbSql.append(" AND "+serchColumn+" <= #{_where.endDate}");
			}
		}
		//停用/启用
		if(null != _where.get("enable")){
			sbSql.append(" AND enable = #{_where.enable}");
		}
		//关键字查询
		if (null != _where.get("selectKey") && !StringUtils.isBlank((String) _where.get("selectValue"))) {
			Integer key =  (Integer)_where.get("selectKey");
			switch(key.intValue()){
				case 1:
					sbSql.append(" AND orgName LIKE CONCAT('%',#{_where.selectValue},'%')");
				break;
				case 2:
					sbSql.append(" AND busiPerson = #{_where.selectValue}");
				break;
				case 3:
					sbSql.append(" AND orgEmail LIKE CONCAT('%',#{_where.selectValue},'%')");
				break;
				case 4:
					sbSql.append(" and recordNo LIKE CONCAT('%',#{_where.selectValue},'%')");
				break;
				default:;
			}
		}
		// 是否为代理机构
		if (null != _where.get("blAgentOrg")){
			sbSql.append(" AND blAgentOrg = #{_where.blAgentOrg}");
		}
	}

	//生成查询分页where条件
	private String constWhere(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//where查询条件
		@SuppressWarnings("unchecked")
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		if(!CollectionUtils.isEmpty(_where)){
			Iterator<Entry<String, Object>> iparam = _where.entrySet().iterator();
			boolean bl = false;
			while(iparam.hasNext()){
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" and ");}
				
				sbSql.append(enty.getKey()+" = "+"#{_where."+enty.getKey()+"}");
				if(!bl)bl = true;
			}
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
}
